Wang Haihua
🍈 🍉🍊 🍋 🍌
基础的卷积神经网络CNN: CNN类似于多层感知器模型MLP因为它们都是前向神经网络,但CNN有几个不同特点
一般使用二维卷积处理图像数据
和MLP中的激活层相同
用于所见参数数量
-卷积和池化层可以连接到全连接层。
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
data = datasets.load_digits()
plt.imshow(data.images[0]) # show first number in the dataset
plt.show()
print('label: ', data.target[0]) # label = '0'
label: 0
X_data = data.images
y_data = data.target
# shape of data
print(X_data.shape) # (8 X 8) format
print(y_data.shape)
(1797, 8, 8) (1797,)
# reshape X_data into 3-D format
# note that this follows image format of Tensorflow backend
X_data = X_data.reshape((X_data.shape[0], X_data.shape[1], X_data.shape[2], 1))
# one-hot encoding of y_data
y_data = to_categorical(y_data)
# partition data into train/test sets
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size = 0.3, random_state = 777)
from keras.models import Sequential
from keras import optimizers
from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D
model = Sequential()
# convolution layer
model.add(Conv2D(input_shape = (X_data.shape[1], X_data.shape[2], X_data.shape[3]), filters = 10, kernel_size = (3,3), strides = (1,1), padding = 'valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
# prior layer should be flattend to be connected to dense layers
model.add(Flatten())
# dense layer with 50 neurons
model.add(Dense(50, activation = 'relu'))
# final layer with 10 neurons to classify the instances
model.add(Dense(10, activation = 'softmax'))
adam = optimizers.Adam(lr = 0.001)
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
history = model.fit(X_train, y_train, batch_size = 50, validation_split = 0.2, epochs = 100, verbose = 0)
history.history.keys()
dict_keys(['val_loss', 'val_accuracy', 'loss', 'accuracy'])
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'validation'], loc = 'upper left')
plt.show()
results = model.evaluate(X_test, y_test)
540/540 [==============================] - 0s 22us/step
print('Test accuracy: ', results[1])
Test accuracy: 0.9870370626449585